// pages/order/order.js
import {
  goodsInfo,
  saveOrder,
  micropay
} from '../../utils/api'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifChecked: false,
    id: '',
    info: {},
    storeId: '',
    storeGuideId: '',
    remark: '',
    disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      storeId: options.storeId
    })
    this.getData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData() {
    goodsInfo(this.data.id).then(res => {
      if (res.subtitle && res.subtitle.includes(',')) {
        res.subtitle = res.subtitle.split(',')
      }
      this.setData({
        info: res
      })
    })
  },
  getStoreGuideId(e) {
    this.setData({
      storeGuideId: e.detail.value
    })
  },
  getRemark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  _confirmSaveOrder() {
    const {
      id,
      storeId,
      info,
      storeGuideId,
      remark,
      disabled
    } = this.data
    if (!disabled) {
      this.setData({
        disabled: true
      })
      return new Promise(resolve => {
        saveOrder({
          goodsId: id,
          storeId,
          paymentAmount: info.price,
          storeGuideId,
          remark
        }).then(res => {
          this.setData({
            disabled: false
          })
          resolve(res.orderNumber)
        }).catch(() => {
          this.setData({
            disabled: false
          })
        })
      })
    }
  },
  async confirmSaveOrder() {
    let that = this
    const {
      id,
      storeId,
      info,
      storeGuideId,
      remark,
      disabled
    } = this.data
    if (!disabled) {
      this.setData({
        disabled: true
      })
      saveOrder({
        goodsId: id,
        storeId,
        paymentAmount: info.price,
        storeGuideId,
        remark
      }).then(rl => {
        micropay({
          openid: app.globalData.openid,
          orderNumber: rl.orderNumber,
        }).then(res => {
          this.setData({
            disabled: false
          })
          wx.requestPayment({
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: 'MD5',
            paySign: res.paySign,
            success(result) {
              wx.redirectTo({
                url: '/pages/useimmed/useimmed?storeId=' + that.data.storeId,
              })
            },
            fail(res) {
              wx.redirectTo({
                url: '/pages/goodsDetail/index?storeId=' + that.data.storeId + '&id=' + that.data.id,
              })
            }
          })
        }).catch(() => {
          this.setData({
            disabled: false
          })
        })
      }).catch(() => {
        this.setData({
          disabled: false
        })
      })
    }
  },
})